﻿<!DOCTYPE>

<html>
<head>
    <title></title>
    <script type="text/javascript">
        var WSURL = "ws://" + location.host + "/Request/WebSocketRequest.ashx";
        var ws = new WebSocket(WSURL);
        ws.onopen = function (e) {
            var Json = "onopen:" + GetObjectJsonInfo(e);
            WriterMessage(Json)
            document.getElementById("btnSubmit").disabled = false;
        }
        ws.onclose = function (e) {
            var Json = "onclose:" + GetObjectJsonInfo(e);
            WriterMessage(Json)
        }
        ws.onmessage = function (e) {
            var Json = "onmessage:" + GetObjectJsonInfo(e);
            WriterMessage(Json)
        }
        ws.onerror = function (e) {
            var Json = "onerror:" + GetObjectJsonInfo(e);
            WriterMessage(Json)
        }
        function SendMessage() {
            var msg = document.getElementById("txtmsg").value;
            ws.send(msg);
        }
        function GetObjectJsonInfo(e) {
            var a = "{";
            for (var obj in e) {
                if (typeof (e[obj]) != "object" && typeof (e[obj]) != "function" && e[obj] != null) {
                    a += obj + ":\"" + e[obj] + "\",";
                }
            }
            a = a.substring(0, a.length - 1) + "}";
            return a;
        }
        function WriterMessage(msg)
        {
            var TempDoc = document.createDocumentFragment();
            var brElement = document.createElement("br");
            var spanElement = document.createElement("span");
            spanElement.innerText = msg;
            TempDoc.appendChild(brElement);
            TempDoc.appendChild(spanElement);
            document.getElementById("div_msg").appendChild(TempDoc);
        }
        window.onload = function () {
            WriterMessage(WSURL);
        }
    </script>
</head>
<body>
    <div>
        <input id="txtmsg" type="text" /><input id="btnSubmit" disabled="disabled" type="button" value="提交" onclick="SendMessage()" />
    </div>
    <div id="div_msg"></div>
</body>
</html>
